package com.yohann.boot.common.validator;

import com.yohann.boot.common.annotation.EnumValid;
import com.yohann.boot.common.enums.ValidEnum;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * 枚举验证器
 *
 * @author Yeung Wah
 * @since 2021/11/20
 */
public class EnumValidator implements ConstraintValidator<EnumValid, Object> {

    /**
     * 枚举class对象
     */
    Class<? extends ValidEnum> clazz;

    @Override
    public void initialize(EnumValid constraintAnnotation) {
        clazz = constraintAnnotation.clazz();
    }

    @Override
    public boolean isValid(Object o, ConstraintValidatorContext constraintValidatorContext) {
        return clazz.isEnum() && clazz.getEnumConstants().length > 0 && clazz.getEnumConstants()[0].valid(o);
    }

}